<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :immediate-check="false"
    >
      <van-cell
        v-for="data in datalist"
        :key="data.filmId"
        @click="handleChangepage(data.filmId)"
      >
        <img :src="data.poster" alt="" />
        <div class="content_text">
          <div class="title">{{ data.name }}</div>
          <div class="content">
            <div :class="data.grade ? '' : 'hidden'">
              观众评分：<span style="color: red">{{ data.grade }}</span>
            </div>
            <div class="acotros">主演：{{ data.actors | actorsFilter }}</div>
            <div>{{ data.nation }} | {{ data.runtime }}分钟</div>
          </div>
        </div>
      </van-cell>
    </van-list>
  </div>
</template>
<script>
import axios from 'axios'
import Vue from 'vue'

Vue.filter('actorsFilter', (data) => {
  if (data === undefined) return '暂无主演'
  return data.map((item) => item.name).join()
})
export default {
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      datalist: '',
      current: 1,
      total: 0
    }
  },
  methods: {
    handleChangepage(id) {
      this.$router.push(`/detail/${id}`)
    },
    onLoad() {
      if (this.datalist.length === this.total && this.total !== 0) {
        this.finished = true
        return
      }
      console.log('触发了')
      this.current++
      axios({
        url: `https://m.maizuo.com/gateway?cityId=${this.$store.state.cityId}&pageNum=${this.current}&pageSize=10&type=1&k=3607599`,
        headers: {
          'X-Client-Info':
            '{"a":"3000","ch":"1002","v":"5.2.1","e":"16737777312402682014793729"}',
          'X-Host': 'mall.film-ticket.film.list'
        }
      }).then((res) => {
        this.datalist = [...this.datalist, ...res.data.data.films]
        this.loading = false
      })
      // console.log(this.datalist)
    }
  },
  mounted() {
    axios({
      url: `https://m.maizuo.com/gateway?cityId=${this.$store.state.cityId}&pageNum=1&pageSize=10&type=1&k=3607599`,
      headers: {
        'X-Client-Info':
          '{"a":"3000","ch":"1002","v":"5.2.1","e":"16737777312402682014793729"}',
        'X-Host': 'mall.film-ticket.film.list'
      }
    }).then((res) => {
      console.log(res.data.data.films)
      this.datalist = res.data.data.films
      this.total = res.data.data.total
    })
  }
}
</script>
<style lang="scss" scoped>
.van-list {
  .van-cell {
    overflow: hidden;
    padding: 0.9375rem;
    img {
      float: left;
      width: 4.125rem;
    }
    .content_text {
      margin-left: 4.625rem;
    }
    .title {
      font-size: 16px;
      font-weight: 700;
    }
    .content {
      font-size: 13px;
      span {
        color: red;
      }
      .acotros {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .hidden {
        visibility: hidden;
      }
    }
  }
}
</style>
